package com.LachesismStorm.lachesism.commonTools;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author lachesism
 * @Date 2025-05-15 14:56
 **/
public class ipUtil {
    public static void main(String[] args) {
        deduplicateIpFile("/Users/yuno/Downloads/ip.txt");
    }
//    public static void main(String[] args) {
//        // 替换成你的文件路径
//        String filePath1 = "/Users/yuno/Downloads/ip_login/login.txt";
//        String filePath2 = "/Users/yuno/Downloads/ip_login/login2.txt";
//
//        // 输出文件路径
//        String outputFilePath = "/Users/yuno/Downloads/ip.txt";
//
//        // 匹配 IPv4 的正则表达式
//        String ipRegex = "\\b(?:[0-9]{1,3}\\.){3}[0-9]{1,3}\\b";
//        Pattern pattern = Pattern.compile(ipRegex);
//
//        // 使用 Set 存储 IP，自动去重
//        Set<String> ipSet = new HashSet<>();
//
//        // 读取文件并提取 IP
//        extractIPsFromFile(filePath1, pattern, ipSet);
//        extractIPsFromFile(filePath2, pattern, ipSet);
//
//        // 写入到 ip.txt 文件中
//        try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {
//            for (String ip : ipSet) {
//                writer.write(ip);
//                writer.newLine();
//            }
//            System.out.println("已成功写入去重后的 IP 到文件: " + outputFilePath);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    // 封装提取 IP 的方法
    private static void extractIPsFromFile(String filePath, Pattern pattern, Set<String> ipSet) {
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;

            while ((line = br.readLine()) != null) {
                Matcher matcher = pattern.matcher(line);
                while (matcher.find()) {
                    ipSet.add(matcher.group());
                }
            }
        } catch (IOException e) {
            System.err.println("读取文件失败：" + filePath);
            e.printStackTrace();
        }
    }
    // 重新从 ip.txt 中读取 IP，去重后覆盖写入（可单独调用）
    public static void deduplicateIpFile(String filePath) {
        Set<String> uniqueIps = new HashSet<>();

        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                uniqueIps.add(line.trim());
            }
        } catch (IOException e) {
            System.err.println("读取 ip.txt 文件失败！");
            e.printStackTrace();
            return;
        }

        // 覆盖写入
        writeIpSetToFile(uniqueIps, filePath);
        System.out.println("已完成 ip.txt 的再次去重并覆盖写入。");
    }
    // 将 IP 集合写入文件（每行一个 IP）
    private static void writeIpSetToFile(Set<String> ipSet, String filePath) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            for (String ip : ipSet) {
                writer.write(ip);
                writer.newLine();
            }
            System.out.println("写入去重 IP 到文件: " + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

}